package com.hcss.core.common.permissions.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hcss.core.annotation.SysLoginLog;
import com.hcss.core.common.permissions.model.UserInfo;
import com.hcss.core.common.permissions.service.UserInfoService;
import com.hcss.core.mvc.BaseController;
import com.hcss.core.mvc.page.Pager;
import com.hcss.core.util.DesEncrypt;
import com.hcss.core.util.SysDictionary;




/**
 * <p>用户信息管理controller、登录</p>
 * 
 * @author lvfang
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/userInfoManage")
@SuppressWarnings("unchecked")
public class UserInfoController extends BaseController{
	@Autowired
	private UserInfoService userInfoService;
	
	/**
	 * 用户管理页面跳转
	 * @return String
	 * @throws Exception 
	 */
	@RequestMapping("/loginToPage")
	public ModelAndView login(HttpServletRequest request) throws Exception{
		ModelAndView view = new ModelAndView("/login");
		return view;
	}
	
	/**
	 * 用户管理页面跳转
	 * @return String
	 * @throws Exception 
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request) throws Exception{
		ModelAndView view = new ModelAndView("/userInfoManage/userInfoList");
		return view;
	}
	/**
	 * 返回所有用户List json数据格式
	 * @return json
	 * @throws Exception 
	 */
	@RequestMapping(value = "/userInfoList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> getAllUsers(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String userName = request.getParameter("userName");
		String userStatus = request.getParameter("userStatus");
		Map<String,Object> parameterMap = request.getParameterMap();
		UserInfo user = new UserInfo();
		user.setUserName(userName);
		user.setUserStatus(userStatus);
		Pager pages = userInfoService.getAllUsers(this.getPager(parameterMap),user);
		return this.getCurrentPager(pages);
	}
	
	/**
	 * 用户解锁
	 * @param user 输入参数
	 * @return Map 提示信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/unlock", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> unlock(@RequestParam("ids") String ids) throws Exception{
		return getMsgTip(userInfoService.unlock(ids));
	}
	
	/**
	 * 用户锁定
	 * @param user 输入参数
	 * @return Map 提示信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/lock", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> lock(@RequestParam("ids") String ids) throws Exception{
		return getMsgTip(userInfoService.lock(ids));
	}
	
	/**
	 * 用户锁定
	 * @param user 输入参数
	 * @return Map 提示信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> resetPwd(@RequestParam("ids") String ids) throws Exception{
		int state = 0;
		try {
			state = userInfoService.executeResetPwd(ids);
		} catch (Exception e) {
			state = 0;
		}
		return getMsgTip(state);
	}
	
	/**
	 * 跳转至用户增加页面
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/addEmployeeToPage")
	public ModelAndView addToUserPage() throws Exception {
		ModelAndView view = new ModelAndView("/userInfoManage/addEmployeeInfo");
		return view;
	}
	
	/**
	 * 保存用户
	 * @param user 输入参数 
	 * @return 提示信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> addUser(HttpServletRequest request,
						HttpServletResponse response,
						@ModelAttribute("userInfo") UserInfo user,
						@RequestParam("userRoleRels") String userRoleRels) throws Exception{
		int state = userInfoService.addUserInfo(user,userRoleRels);
		return this.getMsgTip(state);
	}
	/**
	 * 跳转至用户编辑页面
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/editToUserPage")
	public ModelAndView editToUserPage(@RequestParam("userId") String userId) throws Exception {
		ModelAndView view = new ModelAndView("/userInfoManage/updateEmployeeInfo");
		return view;
	}
	/**
	 * 保存用户
	 * @param user 输入参数 
	 * @return 提示信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/editUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> editUser(HttpServletRequest request,
						HttpServletResponse response,
						@ModelAttribute("userInfo") UserInfo user,
						@RequestParam("userRoleRels") String userRoleRels) throws Exception{
		int state = userInfoService.editUserInfo(user,userRoleRels);
		return this.getMsgTip(state);
	}
	
	/**
	 * 批量删除角色
	 * @param ids 输入参数 角色ID
	 * @return Map 提示信息
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> deleteUser(@RequestParam("ids") String ids)
			throws Exception {
		return getMsgTip(userInfoService.deleteUser(ids));
	}
	
	/**
	 * @Title: validateUserNameIsExist
	 * <p>验证用户名是否存在<p>
	 * @param userName
	 * @param userId
	 * @return
	 * @return: Map<String,String>
	 */
	@RequestMapping(value = "/validateUserNameIsExist", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> validateUserNameIsExist(@RequestParam("userName") String userName,
			@RequestParam("userId") int userId){
		return this.getMsgTip(userInfoService.findUserNameIsExist(userName, userId)>0?1:0);
	}
	
	/**
	 * @Title: validateOldPwdeIsRight
	 * <p>验证输入的原始密码是否正确<p>
	 * @param pwd
	 * @return
	 * @throws Exception
	 * @return: Map<String,String>
	 */
	@RequestMapping(value = "/validateOldPwdIsRight", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,String> validateOldPwdIsRight(@RequestParam("pwd") String pwd) throws Exception{
		String oldPwd = this.getUserInfo().getUserPwd();
		DesEncrypt des = new DesEncrypt();
		String pagePwd = des.encrypt(pwd);
		if(oldPwd.equals(pagePwd)){
			return this.getMsgTip(1);
		}
		return this.getMsgTip(0);
	}
	/**
     * 提交登陆信息
     * 
     * @param user 输入参数
     * @return Map 提示信息
     * @throws Exception
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    @SysLoginLog(loginType = "login")
    public Map<String, String> login(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("user")
    UserInfo user) throws Exception {
        int status = 0;
        HashMap<String, String> map = new HashMap<String, String>();
        status = userInfoService.Login(user);
        if (status != 0) {
            UserInfo userInfo = userInfoService.getLoginUserInfos(user);
            request.getSession().setAttribute(SysDictionary.LOGIN_USERINFO, userInfo);
            userInfoService.saveLog(request, "in");
        }
        return getMsgTipByLoginStatus(status,map);
    }

    /**
     * 注销
     * 
     * @return Map 提示信息
     * @throws Exception
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    @SysLoginLog(loginType = "logOut")
    public Map<String, String> logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
    	userInfoService.saveLog(request, "out");
        request.getSession().removeAttribute(SysDictionary.LOGIN_USERINFO);
        return getMsgTip(1);
    }
}